//
//  XVCIndexSelect.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/16.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCIndexSelect.h"
#import "XXTool.h"
#import "RXMLExhibitorIndexModel.h"
#import "RXMLCountryIndex.h"
#import "RXMLExhibitorInformationModel.h"
#import "ExhibitorsDetailsViewController.h"
#import "XVCProductSeachResultsShow.h"

@interface XVCIndexSelect ()<XVVIndexSelectUseDelegate>

@property (nonatomic,strong) XVVIndexSelectUse* selectView;

@end

@implementation XVCIndexSelect
-(XVVIndexSelectUse*)selectView
{
    if (_selectView==nil)
    {
        _selectView = [[XVVIndexSelectUse alloc] init];
        _selectView.backButton.hidden = YES;
        _selectView.delegate = self;
        [self.view addSubview:_selectView];
    }
    return _selectView;
}

-(void)initParameter
{

}

-(void)initUI
{
    [self selectView];
    if (self.isCountryShow)
    {
        [self.navigationItem setTitle:NSLocalizedString(@"国家", nil)];
        
        RLMResults *topCategory = [RXMLCountryIndex objectsInRealm:[RLMRealm realmWithPath:[XXTool dataBasePath]] where:@"index_father = '0'"];
        NSMutableArray <RXMLExhibitorIndexModel*> * dataList = [NSMutableArray array];
        for (RXMLExhibitorIndexModel* temp in topCategory)
        {
            [dataList addObject:temp];
        }
        [_selectView setIndexDataList:dataList];
        
    }
    else
    {
        [self.navigationItem setTitle:NSLocalizedString(@"类别", nil)];
        
        RLMResults *topCategory = [RXMLExhibitorIndexModel objectsInRealm:[RLMRealm realmWithPath:[XXTool dataBasePath]] where:@"index_father = '0'"];
        NSMutableArray <RXMLExhibitorIndexModel*> * dataList = [NSMutableArray array];
        for (RXMLExhibitorIndexModel* temp in topCategory)
        {
            [dataList addObject:temp];
        }
        [_selectView setIndexDataList:dataList];
    }
}

-(void)refreshLayout:(CGRect)frame
{
    _selectView.frame = CGRectMake(0, 0, frame.size.width
                                   , frame.size.height);
}

#pragma mark XVVIndexSelectUseDelegate
-(void)indexSelectUse:(XVVIndexSelectUse *)indexView dataList:(NSArray *)list clickTableviewIndexPathRow:(NSInteger)row
{
    if ([list[row] isKindOfClass:[RXMLExhibitorIndexModel class]]||[list[row] isKindOfClass:[RXMLCountryIndex class]]) //国家和索引展示
    {
        RXMLExhibitorIndexModel *temp = list[row];
        
        if (temp.index_have_fa==0)//有子类展示子类
        {
            RLMResults * smallCategory = [RXMLExhibitorIndexModel objectsInRealm:[RLMRealm realmWithPath:[XXTool dataBasePath]] where:[NSString stringWithFormat:@"index_father = '%@'",temp.index_child]];
            NSMutableArray <RXMLExhibitorIndexModel*> * dataList = [NSMutableArray array];
            for (RXMLExhibitorIndexModel*temp in smallCategory)
            {
                [dataList addObject:temp];
            }
            [XVVIndexSelectUse newIndexSelectUseWithSourceVC:self
                                               indexDataList:dataList
                                                     useType:XVVIndexSelectShowCategory completion:^{
                                                         
                                                     }];
        }
        else
        {
            if (self.isCountryShow)  //国家搜索
            {
                if (_nowType==XVCIndexSelectUseSearchExhibitor) //搜索对应展商
                {
                    NSString *conditionString = @"";
                    
                    conditionString = [NSString stringWithFormat:@"Country = '%@'",temp.index_name_en];
                    
                    RLMResults *satifiEx = [RXMLExhibitorInformationModel objectsInRealm:[RLMRealm realmWithPath:[XXTool dataBasePath]] where:conditionString];
                    NSMutableArray <RXMLExhibitorIndexModel*> * dataList = [NSMutableArray array];
                    for (RXMLExhibitorIndexModel*temp in satifiEx)
                    {
                        [dataList addObject:temp];
                    }
                    [XVVIndexSelectUse newIndexSelectUseWithSourceVC:self
                                                       indexDataList:dataList
                                                             useType:XVVIndexSelectShowSearchResult completion:^{
                                                                 
                                                             }];
                }
                else  //搜索对应展品
                {
                    
                    [XVCProductSeachResultsShow pushProductSeachResultsShowVCWithSourceVC:self searchKey:temp.index_name_en searchType:@"2"];
                }
            }
            else  //类别搜索
            {
                if (_nowType==XVCIndexSelectUseSearchExhibitor) //搜索对应展商
                {
                    RLMResults *satifiEx = [RXMLExhibitorInformationModel objectsInRealm:[RLMRealm realmWithPath:[XXTool dataBasePath]] where:[NSString stringWithFormat:@"ProductIndex CONTAINS '%@'",temp.index_NO]];
                    NSMutableArray <RXMLExhibitorIndexModel*> * dataList = [NSMutableArray array];
                    for (RXMLExhibitorIndexModel*temp in satifiEx)
                    {
                        [dataList addObject:temp];
                    }
                    [XVVIndexSelectUse newIndexSelectUseWithSourceVC:self
                                                       indexDataList:dataList
                                                             useType:XVVIndexSelectShowSearchResult completion:^{
                                                                 
                                                             }];
                }
                else if(_nowType == XVCIndexSelectUseSearchProduct)//搜索对应展品
                {
                    [XVCProductSeachResultsShow pushProductSeachResultsShowVCWithSourceVC:self searchKey:temp.index_NO searchType:@"3"];
                }else{
                    // 从 个人资料跳转 而来
                        if(self.isProfile){
                            if([XXTool didCurrentLanguageChinese]){
                                //self.info=temp.index_name_cn;
                                
                              NSString*  string = [@"" stringByAppendingFormat:@"%@-%@", temp.index_name_cn, temp.index_NO ];
                                _Pblock(string);
                                //NSLog(@"返回的东西%@",string);
                            }else{
                                //self.info=temp.index_name_en;
                                NSString*  string = [@"" stringByAppendingFormat:@"%@-%@", temp.index_name_en, temp.index_NO ];
                                _Pblock(string);
                            }
                            [self.navigationController popViewControllerAnimated:YES];
                            return;
                        }
                }
            }
        }

    }
    else if ([list[row] isKindOfClass:[RXMLExhibitorInformationModel class]]) //对应展商详情展示
    {
        
        RXMLExhibitorInformationModel *temp = list[row];
        [ExhibitorsDetailsViewController pushViewControllerWithSourceVC:self exhibitorInfo:temp];
    }
}

+(void)pushIndexSelectVCWithSourceVCProfile:(UIViewController *)sourceVC isUseCountryShow:(BOOL)is nowType:(XVCIndexSelectType)type Compeletation:(void (^)(NSString *))block{
    XVCIndexSelect *vc = [[XVCIndexSelect alloc] init];
    vc.isCountryShow = is;
    vc.nowType = type;
    vc.isProfile=true;
    vc.Pblock=block;
       if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }

}

#pragma mark api
+(void)pushIndexSelectVCWithSourceVC:(UIViewController *)sourceVC isUseCountryShow:(BOOL)is nowType:(XVCIndexSelectType)type
{
    XVCIndexSelect *vc = [[XVCIndexSelect alloc] init];
    vc.isCountryShow = is;
    vc.isProfile=false;
    vc.nowType = type;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
